from itsdangerous import TimedJSONWebSignatureSerializer
from django.conf import settings



def generate_verify_email_url(name,email):
    """
    生成邮箱验证链接 对用户姓名和邮箱信息加密
    :param name:
    :param email:
    :return:
    """
    jiami = TimedJSONWebSignatureSerializer(settings.SECRET_KEY,expires_in=24*3600)
    data = {'username':name,'email':email}
    token = jiami.dumps(data).decode()
    url = settings.EMAIL_VERIFY_URL+token
    return url
def check_verify_email_url(token):
    """
    解码出用户姓名和邮箱进行验证
    :param name:
    :param email:
    :return:
    """
    jiemi = TimedJSONWebSignatureSerializer(settings.SECRET_KEY,expires_in=24*3600)
    try:
        data_dict = jiemi.loads(token)
    except Exception:
        return None
    username = data_dict.get('username')
    email = data_dict.get('email')
    return (username,email)